<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-calendar"></i> 个人密码
                </el-breadcrumb-item>
                <el-breadcrumb-item>我的密码</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="form-box">
                <el-form ref="form" :model="form" label-width="80px">
                    <el-form-item label="旧密码" >
                        <el-input v-model="param.old_password" style="width:30% "  type="password"
                                  placeholder="password"></el-input>
                    </el-form-item>
                    <el-form-item label="新密码" style="width:40% " >
                        <el-input v-model="param.new_password"  type="password"
                                  placeholder="password"></el-input>
                    </el-form-item>
                    <el-form-item label="确认密码" style="width:40% " >
                        <el-input v-model="param.new_password2"  type="password"
                                  placeholder="password"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="onSubmit">确定</el-button>
                        <el-button @click="onCancel">取消</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
    import {updatepwd} from "../../api/admin"
    export default {
        data() {
            return {
                form:{},
                param: {
                    old_password: '',
                    new_password: '',
                    new_password2:''
                }
            };
        },
        methods: {
            onCancel(){
                this.param.old_password='';
                this.param.new_password='';
                this.param.new_password2='';
            },
            onSubmit() {
              let user= JSON.parse(localStorage.getItem('user'));
              console.log(user.userpassword);
              console.log(this.param.old_password);
               if (!(user.userpassword.trim()===this.param.old_password.trim())){
                   this.$message.error('原密码不正确');
                   this.param.old_password='';
                   this.param.new_password='';
                   this.param.new_password2='';
               }else if ((this.param.old_password.trim()==this.param.new_password.trim())){
                   this.$message.error('不能使用近期使用的密码！');
                   this.param.old_password='';
                   this.param.new_password='';
                   this.param.new_password2='';
               }else if (!(this.param.new_password.trim()==this.param.new_password2.trim())){
                   this.$message.error('密码不一致,请重新输入密码！');
                   this.param.old_password='';
                   this.param.new_password='';
                   this.param.new_password2='';
               }else{
                  updatepwd(JSON.stringify({'userpassword':this.param.new_password.trim(),'userid':user.userid})).then(res =>{
                      if (res.state==2000){
                          this.$message.success(res.message+',请重新登录！');
                          localStorage.removeItem('user');
                          this.$router.push('/login');
                      }else{
                          this.$message.error(res.message);
                      }
                  })
               }
            }
        }
    };
</script>
